package cn.ito.zjgsu.domain.wrapper;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.ito.zjgsu.domain.Department;
import cn.ito.zjgsu.domain.FinancialType;
import cn.ito.zjgsu.domain.IndustryType;

public class DepartmentWrapper {
	
	private Department department;
	private FinancialTypeWrapper financialTypeWrapper;
	private IndustryTypeWrapper industryTypeWrapper;
	private List<DepartmentWrapper> sonDepartments;
	private DepartmentWrapper parentDepartment;
	
	
	
	

	



	public FinancialTypeWrapper getFinancialTypeWrapper() {
		return financialTypeWrapper;
	}



	public void setFinancialTypeWrapper(FinancialTypeWrapper financialTypeWrapper) {
		this.financialTypeWrapper = financialTypeWrapper;
	}



	public IndustryTypeWrapper getIndustryTypeWrapper() {
		return industryTypeWrapper;
	}



	public void setIndustryTypeWrapper(IndustryTypeWrapper industryTypeWrapper) {
		this.industryTypeWrapper = industryTypeWrapper;
	}



	public DepartmentWrapper getParentDepartment() {
		return parentDepartment;
	}

	

	@Override
	public String toString() {
		return "DepartmentWrapper [department=" + department + ", sonDepartments=" + sonDepartments + "]";
	}



	public void setParentDepartment(DepartmentWrapper _parentDepartment) {
		if(this.parentDepartment == null){
			this.parentDepartment = new DepartmentWrapper();
		}	
		_parentDepartment.addSonDepartment(this);
		this.parentDepartment = _parentDepartment;
	}

	public List<DepartmentWrapper> getSonDepartments() {
		return sonDepartments;
	}

	public void setSonDepartments(List<DepartmentWrapper> sonDepartments) {
		if(this.sonDepartments == null){
			this.sonDepartments = new ArrayList<DepartmentWrapper>();
		}	
		this.sonDepartments = sonDepartments;
	}
	
	public void addSonDepartment(DepartmentWrapper sonDepartment) {
		if(this.sonDepartments == null){
			this.sonDepartments = new ArrayList<DepartmentWrapper>();
		}	
		this.sonDepartments.add(sonDepartment);
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public DepartmentWrapper() {
	}

	public DepartmentWrapper(Department department) {
		this.department = department;
	}

	public String toJsonObjectString() {
		return toJsonObject().toJSONString();
	}

	public JSONObject toJsonObject() {
		JSONObject json = new JSONObject();
		initDepartmentField(json);
		return json;
	}

	
	
	private void initDepartmentField(JSONObject json) {
		json.put("AreaName", this.getDepartment().getDepartmentAreaName());
		json.put("CreateTime", this.getDepartment().getDepartmentCreateTime());
		json.put("Fax", this.getDepartment().getDepartmentFax());
		json.put("Id", this.getDepartment().getDepartmentId());
		json.put("Name", this.getDepartment().getDepartmentName());
		json.put("Phone", this.getDepartment().getDepartmentPhone());
		
		if(this.getSonDepartments() == null){
			json.put("sonDepartment", null);
		}else{
			json.put("sonDepartment", formatsonDepartments(this));
		}
		if(financialTypeWrapper != null){
			json.put("financialType", financialTypeWrapper);
		}
		if(industryTypeWrapper != null){
			json.put("industryType", industryTypeWrapper);
		}
	
	}

	private JSONArray formatsonDepartments(DepartmentWrapper _parentDepartments) {	
		if (_parentDepartments != null) {
			JSONArray array = new JSONArray();
			if (_parentDepartments.getSonDepartments() == null) {
				return null;
			} else {
				for (DepartmentWrapper dw : _parentDepartments.getSonDepartments()) {
					if (dw.getSonDepartments() == null) {
						array.add(dw.toJsonObject());
					}else{
						array.add(formatsonDepartments(dw));
					}				
				}

			}
			return array;
		}
		return null;
	}

	public static JSONArray toJsonArray(List<DepartmentWrapper> departmentWrappers) {
		JSONArray array = new JSONArray();
		for (DepartmentWrapper dw : departmentWrappers) {
			array.add(dw.toJsonObject());
		}
		return array;
	}

	public static String toJsonArrayString(JSONArray array) {
		return array.toJSONString();
	}

	public static String toJsonArrayString(List<DepartmentWrapper> departmentWrappers) {
		return toJsonArray(departmentWrappers).toJSONString();
	}
	
	public static List<DepartmentWrapper> wrapDepartment(List<Department> departments){
		List<DepartmentWrapper> roots = new ArrayList<DepartmentWrapper>();
		Map<Integer,DepartmentWrapper> dwmap = new LinkedHashMap<Integer,DepartmentWrapper>();
		for(Department d : departments){
			dwmap.put(d.getDepartmentId(), new DepartmentWrapper(d));
		}
		
		for(Map.Entry<Integer,DepartmentWrapper> map : dwmap.entrySet()){
			if(map.getValue().getDepartment().getDepartmentParentid() == null){
				roots.add(map.getValue());			
			}else if (map.getValue().getDepartment().getDepartmentParentid() != null){
				int pdid = map.getValue().getDepartment().getDepartmentParentid();
				DepartmentWrapper pdw = dwmap.get(pdid);
				dwmap.get(map.getKey()).setParentDepartment(pdw);
			}
		}
		return roots;
	}

	
}
